﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Hr
{
    public partial class frmManageEmployee : BaseForm.frmBaseMdiBusinessForm
    {
        #region Var
        private MstPersonEntity mSelectedPerson;
        private bool showBirthday=false;
        #endregion

        #region Ctor
        public frmManageEmployee()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbGender, "GENDER", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colGender, "GENDER", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "PS", true);
            cmbStatus.SelectedIndex = 1;
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity department = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND P.DEPARTMENT_ID = '" + department.DepartmentId + "' ";
            }
            if (txtPosition.Tag != null && txtPosition.Tag.GetType() == typeof(MstPositionEntity))
            {
                MstPositionEntity position = (MstPositionEntity)txtPosition.Tag;
                condition += " AND P.POSITION_ID = '" + position.PositionId + "' ";
            }
            if (txtSupervisor.Tag != null && txtSupervisor.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity supervisor = (MstPersonEntity)txtSupervisor.Tag;
                condition += " AND P.SUPERVISOR = '" + supervisor.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(txtPersonCode.Text) == false)
            {
                condition += " AND P.PERSON_CODE LIKE '%" + txtPersonCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtPersonNameCn.Text) == false)
            {
                condition += " AND P.NAME_CN LIKE '%" + txtPersonNameCn.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbGender.Text) == false && cmbGender.SelectedValue != null)
            {
                condition += " AND P.GENDER = '" + cmbGender.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND P.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            else
            {
                condition += " AND P.STATUS != 'PS09' AND P.STATUS != 'PS02'";
            }
            if (string.IsNullOrEmpty(cmbIsSocialSecurity.Text) == false)
            {
                if (cmbIsSocialSecurity.Text == "是")
                {
                    condition += " AND E.IS_SOCIAL_SECURITY = 1";
                }
                else
                {
                    condition += " AND (E.IS_SOCIAL_SECURITY = 0 OR E.IS_SOCIAL_SECURITY IS NULL)";
                }
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "PERSON_CODE";
            DataTable dt = MstPersonBs.GetPersonByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnSubmit.Enabled = false; //创建用户
            btnDelete.Enabled = false;
            btnApprove.Enabled = false;//客户分配
            btnPrint.Enabled = false;//续签合同
            btnExport.Enabled = false;//调薪
            btnFormal.Enabled = false;//转正
            btnLeave.Enabled = false;//请假
            btnTravel.Enabled = false;//出差
            btnCarRent.Enabled = false;//用车申请
            btnPersonLevel.Enabled = false;//离职
            if (mSelectedPerson == null)
            {
                return;
            }
            switch (mSelectedPerson.Status)
            {
                case "PS01"://在职
                case "PS03"://停薪留职
                case "PS04"://停职留薪
                case "PS05"://带薪休假
                case "PS06"://无薪休假
                    btnModify.Enabled = true;
                    btnSubmit.Enabled = true;
                    btnApprove.Enabled = true;
                    btnPrint.Enabled = true;
                    btnExport.Enabled = true;
                    btnLeave.Enabled = true;
                    btnTravel.Enabled = true;
                    btnCarRent.Enabled = true;
                    btnPersonLevel.Enabled = true;
                    break;
                case "PS02"://离职
                    btnModify.Enabled = true;
                    btnDelete.Enabled = true;
                    break;
                case "PS07"://试用期
                    btnFormal.Enabled = true;
                    btnModify.Enabled = true;
                    btnSubmit.Enabled = true;
                    btnApprove.Enabled = true;
                    btnPrint.Enabled = true;
                    btnExport.Enabled = true;
                    btnLeave.Enabled = true;
                    btnTravel.Enabled = true;
                    btnCarRent.Enabled = true;
                    btnPersonLevel.Enabled = true;
                    btnDelete.Enabled = true;
                    break;
                case "PS09"://已删除
                    break;
            }
        }

        #endregion

        #region Events
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            if (showBirthday)
            {
                frmMaintainBirthdayAlert frm = new frmMaintainBirthdayAlert();
                frm.ViewOnly = true;
                frm.ShowDialog();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainEmployee frm = new frmMaintainEmployee();
            frm.ThisActionType = ActionType.View;
            frm.SelectedPerson = mSelectedPerson;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainEmployee frm = new frmMaintainEmployee();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainEmployee frm = new frmMaintainEmployee();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedPerson = mSelectedPerson;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中员工？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = MstPersonBs.DeletePerson(mSelectedPerson);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Submit()//创建用户
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SecUserEntity user = SecUserBs.GetUserByPersonID(mSelectedPerson.PersonId);
            if (user != null)
            {
                MessageBox.Show("该员工已经生成过登陆用户，不能重复生成用户名！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            user = new SecUserEntity();
            user.UserId = Guid.NewGuid().ToString();
            user.PersonId = mSelectedPerson.PersonId;
            user.UserAccount = mSelectedPerson.PersonCode;
            user.ChineseName = mSelectedPerson.NameCn;
            user.EnglishName = mSelectedPerson.NameEn;
            user.Password = Encryption.Encrypt(CommonParameter.DefaultPassword);
            user.PasswordModifyDate = DateTime.Now;
            user.IsDelete = 0;
            user.IsLock = 0;
            user.Position = mSelectedPerson.PositionId;
            user.UserType = 0;
            user.MobilePhone = mSelectedPerson.Mobile;
            user.Phone = mSelectedPerson.Telephone;
            user.Email = mSelectedPerson.Email;
            user.Address = mSelectedPerson.Address;
            user.Gender = 0;
            user.CityId = mSelectedPerson.City;
            user.ResponsibleArea = "";
            user.Department = mSelectedPerson.DepartmentId;
            user.ValidateType = 0;
            user.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            user.LastModifiedTime = DateTime.Now;

            try
            {
                if (SecUserBs.InsertUser(user))
                {
                    BindDataGrid();
                    MessageBox.Show("生成成功", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("生成失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("生成失败\n" + ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()//客户分配
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCustomerAssignment frm = new frmMaintainCustomerAssignment(mSelectedPerson);
            frm.ShowDialog();
        }

        protected override void Click_Print()//续签合同
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainRenewContract frm = new frmMaintainRenewContract(mSelectedPerson);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        protected override void Click_Import()//合同历史
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainContractHistory frm = new frmMaintainContractHistory(mSelectedPerson);
            frm.ViewOnly = true;
            frm.ShowDialog();
        }

        protected override void Click_Export()//调薪历史
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainSalaryChangedHistory frm = new frmMaintainSalaryChangedHistory(mSelectedPerson);
            frm.ViewOnly = true;
            frm.ShowDialog();
        }

       
        #endregion

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >=0)
            {
                DataRow dr = view.GetDataRow(e.RowHandle);
                HrEmployeeEntity employee = new HrEmployeeEntity(dr);
                MstPersonEntity person = new MstPersonEntity(dr);
                if (employee != null && employee.ContractEndDate <= DateTime.Today.AddMonths(1) && person.Status != "PS02" && person.Status != "PS09")
                {
                    e.Appearance.ForeColor = Color.Red;
                }
                else
                {
                    e.Appearance.ForeColor = Color.Black;
                }
            }
        }

        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                DataRow dr = view.GetDataRow(e.RowHandle);
                MstPersonEntity person = new MstPersonEntity(dr);
                if (person.Birthday != null && person.Birthday.Length >= 5 && person.Birthday.Substring(5) == DateTime.Today.ToString("MM-dd"))
                {
                    e.Appearance.BackColor = Color.Blue;
                    showBirthday = true;
                }
            }
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
                txtDepartment.Tag = frm.SelectedDepartment;
            }
            else
            {
                txtDepartment.Text = "";
                txtDepartment.Tag = null;
            }
        }

        private void txtPosition_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainPositionQuery frm = new frmMaintainPositionQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPosition != null)
            {
                txtPosition.Text = frm.SelectedPosition.PositionName;
                txtPosition.Tag = frm.SelectedPosition;
            }
            else
            {
                txtPosition.Text = "";
                txtPosition.Tag = null;
            }
        }

        private void txtSupervisor_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtSupervisor.Text = frm.SelectedPerson.NameCn;
                txtSupervisor.Tag = frm.SelectedPerson;
            }
            else
            {
                txtSupervisor.Text = "";
                txtSupervisor.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPerson = new MstPersonEntity(dr);
        }

        private void btnSalaryChanged_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//调薪申请
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainSalaryApplication frm = new frmMaintainSalaryApplication(mSelectedPerson);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnFormal_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//转正申请
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainFormalApplication frm = new frmMaintainFormalApplication(mSelectedPerson);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnLeave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//请假申请
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainLeaveApplication frm = new frmMaintainLeaveApplication(mSelectedPerson);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnTravel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//出差申请
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainTravelApplication frm = new frmMaintainTravelApplication(mSelectedPerson);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnCarRent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//用车申请
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCarRentingApplication frm = new frmMaintainCarRentingApplication(mSelectedPerson);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnPersonLevel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//离职申请
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainEmployeeLeaveApplication frm = new frmMaintainEmployeeLeaveApplication(mSelectedPerson);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnPersonPosition_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//调职申请
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            HrPersonPositionApplicationEntity entity = new HrPersonPositionApplicationEntity();
            entity.ApplicationPerson = Common.CommonParameter.CurrentUser.PersonId;
            entity.PromotionPerson = mSelectedPerson.PersonId;
            entity.ApplicationDate = DateTime.Now;

            frmMaintainEmployeePositionApplication frm = new frmMaintainEmployeePositionApplication(entity);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnExportData_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(sfd.FileName);
            }
        }

    }
}
